"----vim 个人使用习惯配置start------
let &t_ut=''
set autochdir
set maxmempattern=2000000
set exrc
set secure
set number
set relativenumber
set cursorline
set hidden
set expandtab
set tabstop=2
set shiftwidth=2
set softtabstop=2
" set ambiwidth=double 
" set autoindent
set list
set listchars=tab:\|\ ,trail:▫
set scrolloff=30
set wrap
set tw=0
set indentexpr=
set foldmethod=indent
set foldlevel=99
set foldenable
set formatoptions-=tc
set splitright
set splitbelow

" 忽略大小写
set ignorecase
set smartcase

set inccommand=split
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
set completeopt=longest,noinsert,menuone,noselect,preview
set ttyfast "should make scrolling faster
set lazyredraw "same as above

set updatetime=100

" ===
" === Terminal Behaviors
" ===
let g:neoterm_autoscroll = 1
autocmd TermOpen term://* startinsert
tnoremap <C-N> <C-\><C-N>
tnoremap <C-O> <C-\><C-N><C-O>
let g:terminal_color_0  = '#000000'
let g:terminal_color_1  = '#FF5555'
let g:terminal_color_2  = '#50FA7B'
let g:terminal_color_3  = '#F1FA8C'
let g:terminal_color_4  = '#BD93F9'
let g:terminal_color_5  = '#FF79C6'
let g:terminal_color_6  = '#8BE9FD'
let g:terminal_color_7  = '#BFBFBF'
let g:terminal_color_8  = '#4D4D4D'
let g:terminal_color_9  = '#FF6E67'
let g:terminal_color_10 = '#5AF78E'
let g:terminal_color_11 = '#F4F99D'
let g:terminal_color_12 = '#CAA9FA'
let g:terminal_color_13 = '#FF92D0'
let g:terminal_color_14 = '#9AEDFE'


set encoding=UTF-8
" leader设置成空格
let mapleader=" "
" 使用鼠标
set mouse=a
" 显示行号
set nu
" 相对行号
set relativenumber
" 改变 vim中光标的形状
let g:db_ui_use_nerd_fonts=1
let &t_SI.="\e[5 q" "SI = INSERT mode
let &t_SR.="\e[4 q" "SR = REPLACE mode
let &t_EI.="\e[1 q" "EI = NORMAL mode (ELSE)

" 光标
set guicursor=n-v-c:block,i-ci-ve:ver25,r-cr:hor20,o:hor50
  \,a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor
  \,sm:block-blinkwait175-blinkoff150-blinkon175

" 显示按下的按键
set showcmd
" 按tab显示菜单
set wildmenu
" 不需要备份文件
" set nobackup
silent !mkdir -p ~/.config/nvim/tmp/backup
silent !mkdir -p ~/.config/nvim/tmp/undo
"silent !mkdir -p ~/.config/nvim/tmp/sessions
set backupdir=~/.config/nvim/tmp/backup,.
set directory=~/.config/nvim/tmp/backup,.
if has('persistent_undo')
	set undofile
	set undodir=~/.config/nvim/tmp/undo,.
endif

" Auto change directory to current dir
" autocmd BufEnter * silent! lcd %:p:h

"----vim 个人使用习惯配置end------
"ssh 远程粘贴板
" if executable('clipboard-provider')
"     let g:clipboard = {
"           \ 'name': 'myClipboard',
"           \     'copy': {
"           \         '+': 'clipboard-provider copy',
"           \         '*': 'env COPY_PROVIDERS=tmux clipboard-provider copy',
"           \     },
"           \     'paste': {
"           \         '+': 'clipboard-provider paste',
"           \         '*': 'env COPY_PROVIDERS=tmux clipboard-provider paste',
"           \     },
"           \ }
" endif
"随机选一个颜色风格
function RandomColorScheme()
  let mycolors = split(globpath(&rtp,"**/colors/*.vim"),"\n") 
  exe 'so ' . mycolors[localtime() % len(mycolors)]
  unlet mycolors
endfunction

"call RandomColorScheme()

:command NewColor call RandomColorScheme()


" 文件类型设置 "{{{
" ---------------------------------------------------------------------
" JSX 语法高亮
" set filetypes as typescriptreact
" autocmd BufNewFile,BufRead *.tsx,*.jsx,*.js set filetype=typescriptreact


" 来自 525312750:局外人
" 合并多行文本到一行
" @line_sep: 分割符，分割前字符相同为一行
" @val_sep: 合并后的值如何分隔
" let d = {} | g/\t/let [k,v] = split(getline('.'), '\t') | if has_key(d,k) | call add(d[k],v) | else | let d[k] = [v] | endif | %d | for [k,v] in items(d) | put = k..\"\t\"..join(v,' ') | endfor | 1d
command! MergeMultiValue call s:merge_multi_value_to_single_line()
function! s:merge_multi_value_to_single_line(line_sep="\t", val_sep=' ')
  function! s:load_line() closure
    " 加入\V 为了和 put 中统一，
    " 关于转义 :help \V
    let [k,v] = split(getline('.'), '\V'..a:line_sep)
    if has_key(d,k)
      call add(d[k],v)
    else
      let d[k] = [v]
    endif
  endfunction

  let d = {}
  execute 'g/\V'..a:line_sep..'/call s:load_line()'

  %d
  for [k,v] in items(d)
    put = k..a:line_sep..join(v,a:val_sep)
  endfor
  1d
endfunction

command! AddFrequency call s:add_frequency_to_dict()
function! s:add_frequency_to_dict(line_sep=" ", val_sep='\t')
  function! s:load_line() closure
    " 加入\V 为了和 put 中统一，
    " 关于转义 :help \V
    let [k,v] = split(getline('.'), '\V'..a:line_sep)
    if has_key(numCount,k)
      " echo(k..len(numCount[k]))
      let numCount[k][v] = 1000 - 4 * len(numCount[k])
    else
      " echo(k..'---'..v)
      let numCount[k] = {}
      let numCount[k][v] = 1000
    endif
  endfunction

  " 存储编码，编码里面存储词组
  let numCount = {}
  execute 'g/\V'..a:line_sep..'/call s:load_line()'

  %d
  for [k,v] in items(numCount)
    for [value,frequency] in items(v)
      put = k..a:val_sep..value..a:val_sep..frequency
    endfor
  endfor
  1d
endfunction
